// pages/user/createCard.js
const api = require("../../utils/api")
const cache = require("../../utils/cache")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    picker: ['身份证', '护照', '香港身份证', '澳门身份证', '台湾身份证'],
    pickerSex: ['男', '女'],
    region: [], //地址
    name: '', //姓名
    cardType: '身份证', //卡类型
    cardNo: '', //卡号
    addrDetail: '', //详细地址
    phone: '', //手机号
    checkNum: '', //验证码
    region_child: [], //地址 儿童
    name_child: '', //姓名 儿童
    cardType_child: '身份证', //卡类型 儿童
    cardNo_child: '', //卡号 儿童
    addrDetail_child: '', //详细地址 儿童
    phone_child: '', //手机号 儿童
    checkNum_child: '', //验证码 儿童
    indexSex: 0,
    delta: 1//返回的页面层数
    },
  handleCreateCard(e) {
    this.setData({
      loadModal: true
    })
    let that = this
    setTimeout(()=>{
      that.setData({
        loadModal: false
      })
    },10000);
    let patient = {
      name:this.data.TabCur==0? this.data.name:this.data.name_child,
      cardno: this.data.TabCur==0?this.data.cardNo:'',
      cardtype: '',
      phone: this.data.TabCur==0?this.data.phone:this.data.phone_child,
      sex: '',
      province: '',
      city: '',
      county: '',
      addrDetail: this.data.TabCur==0?this.data.addrDetail:this.data.addrDetail_child,
      child: '',
      guardianCardtype: '',
      guardianCardno: '',
      crtFlag: '',
      ioNo: '',
      checkNum: this.data.TabCur==0?this.data.checkNum:this.data.checkNum_child,
    }
    if (this.data.TabCur == 0) { //大人小孩
      patient.child = "N"
      if (this.data.cardType == "身份证") {
        patient.cardtype = "01"
      } else if (this.data.cardType == "护照") {
        patient.cardtype = "02"
      } else if (this.data.cardType == "香港身份证") {
        patient.cardtype = "03"
      } else if (this.data.cardType == "澳门身份证") {
        patient.cardtype = "04"
      } else if (this.data.cardType == "台湾身份证") {
        patient.cardtype = "05"
      }
      if (this.data.region.length >= 3) {
        patient.province = this.data.region[0]
        patient.city = this.data.region[1]
        patient.county = this.data.region[2]
      }
      if (patient.cardtype == "01" && patient.cardno != '') {
        let sexBit = patient.cardno.substr(16, 1);
        if (sexBit % 2 == 0) { //男单女双
          patient.sex = "M"
        } else {
          patient.sex = "F"
        }
      }
    }else{
        patient.child = "Y"
        if (this.data.cardType_child == "身份证") {
          patient.guardianCardtype = "01"
        } else if (this.data.cardType_child == "护照") {
          patient.guardianCardtype = "02"
        } else if (this.data.cardType_child == "香港身份证") {
          patient.guardianCardtype = "03"
        } else if (this.data.cardType_child == "澳门身份证") {
          patient.guardianCardtype = "04"
        } else if (this.data.cardType_child == "台湾身份证") {
          patient.guardianCardtype = "05"
        }
        patient.guardianCardno = this.data.cardNo_child
        if (this.data.region_child.length >= 3) {
          patient.province = this.data.region_child[0]
          patient.city = this.data.region_child[1]
          patient.county = this.data.region_child[2]
        }
        if (this.data.pickerSex[this.data.indexSex] == "男") {
          patient.sex = "F"
        } else {
          patient.sex = "M"
        }
    }
    
    api.createPatient(patient).then(res => {
      let cachePt = cache.getValue("patient");
      patient.ioNo = res.ioNo;
      patient.crtFlag = 'Y'
      if (cachePt == undefined || cachePt == null || cachePt == '') {
        let arr = [patient]
        cache.setValue("patient", arr);
      } else {
        cachePt.forEach(kk=>{
          kk.crtFlag = 'N'
        })
        cachePt.push(patient)
        cache.setValue("patient", cachePt)
      }
      that.setData({
        loadModal: false
      })
      wx.navigateBack({
        delta: that.data.delta
      });
    }).catch(e=>{
      that.setData({
        loadModal: false
      })
    })
    
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
    })
  },
  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value
    })
  },
  PickerChangeSex(e) {
    console.log(e);
    this.setData({
      indexSex: e.detail.value
    })
  },
  RegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  RegionChangeChild: function (e) {
    this.setData({
      region_child: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.delta != undefined && options.delta!=null && options.delta!=''){
      this.data.delta = parseInt(options.delta)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})